import { createRouter, createWebHashHistory } from 'vue-router'
import NProgress from 'nprogress'

const routes = [
  {
    path: '/room',
    name: 'room',
    component: () => import('../views/ChatRoom.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/LoginView.vue')
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

const whiteList = ["/login"]
router.beforeEach((to, from, next) => {
  NProgress.start()
  let token = localStorage.getItem("token")
  if (token) {
    if (whiteList.includes(to.fullPath)) {
      next(from.path)
    }
    next()
  } else {
    if (whiteList.includes(to.path)) {
      console.log('在白名单中 放行');
      next()
    } else {
      next({ path: '/login' })
    }
  }
  NProgress.done()
})

export default router
